/*
 * $Id: AlbumList.java 340 2008-04-14 01:20:00Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;
import org.devtcg.five.music.widget.AlphabetListView;
import org.devtcg.five.provider.Five;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class AlbumList extends Activity
{
	private static final String TAG = "AlbumList";

	private static final String PROJECTION[] = {
		"_id", "name", "full_name", "artwork", "artist"
	};
	
	private Cursor mCursor;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);        
        setContentView(R.layout.album_list);

        Intent i = getIntent();
        if (i.getData() == null)
        	i.setData(Uri.parse("content://org.devtcg.five/media/music/albums/complete"));

        if (i.getAction() == null)
        	i.setAction(Intent.VIEW_ACTION);

        mCursor = managedQuery(i.getData(), PROJECTION, null, null, "a.name COLLATE UNICODE ASC");

		AlphabetListView list = (AlphabetListView)findViewById(R.id.album_list);

//		list.setAdapterParameters(
//		  R.layout.artist_list_item, R.layout.alphabet_separator, mCursor,
//		  new String[] { "artwork", "full_name" },
//		  new int[] { R.id.artist_photo, R.id.artist_name },
//		  mCursor.getColumnIndex("name")
//		);

        list.setAdapterParameters(
          R.layout.album_list_item, R.layout.alphabet_separator, mCursor,
          new String[] { "artwork", "full_name", "artist" },
          new int[] { R.id.album_cover, R.id.album_name, R.id.artist_name },
          mCursor.getColumnIndex("name")
        );
        
        list.getAdapter().setViewBinder(mDefaultAlbumCover);
        list.getListView().setOnItemClickListener(mClickEvent);
    }

	private final SimpleCursorAdapter.ViewBinder mDefaultAlbumCover = new SimpleCursorAdapter.ViewBinder()
	{
		public boolean setViewValue(View v, Cursor c, int col)
		{
			if (v instanceof ImageView)
			{
				String value = c.getString(col);

				ImageView iv = (ImageView)v;

				if (value == null)
					iv.setImageResource(R.drawable.lastfm_cover_small);
				else
					iv.setImageURI(Uri.parse(value));

				return true;
			}

			return false;
		}
	};
    
    private final OnItemClickListener mClickEvent = new OnItemClickListener()
    {
		public void onItemClick(AdapterView adapter, View v, int pos, long id)
		{
			Intent i = getIntent();

			/* Clicked on a separator row. */
			if (id == -1)
				return;

			Uri chosen = Five.Music.Albums.CONTENT_URI.buildUpon()
			  .appendPath(String.valueOf(id))
			  .build();

			String action = i.getAction();

			Log.d(TAG, "uri=" + chosen.toString());
			Log.d(TAG, "action=" + action);

			if (action.equals(Intent.PICK_ACTION) == true ||
			    action.equals(Intent.GET_CONTENT_ACTION) == true)
			{
				setResult(RESULT_OK, chosen.toString());
				finish();
			}
			else
			{
				startActivity(new Intent(Intent.VIEW_ACTION, chosen, AlbumList.this, AlbumSongList.class));
			}
		}
    };
}
